package csc.daonjpa.java.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import csc.daonjpa.java.dao.CustomerDAO;
import csc.daonjpa.java.dao.TransactionDAO;
import csc.daonjpa.java.domain.Customer;
import csc.daonjpa.java.domain.Transaction;

@Controller
@RequestMapping(value="/profile")
public class ProfileController {

	@Autowired
	private CustomerDAO customerDAO;
	
	@Autowired
	private TransactionDAO transactionDAO;
	
	@ModelAttribute("customer")
	private Customer contructCustomer(){
		return new Customer();
	}
	
	@RequestMapping
	public String goProfile(Model model){
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String loginId = auth.getName();
		Customer customer = customerDAO.getCustomer(loginId);
		model.addAttribute("customer", customer);
		
		List<Transaction> transactionSource =  transactionDAO.getTransactionsSource(loginId);
		model.addAttribute("transactionSource", transactionSource);
		
		List<Transaction> transactionTarget =  transactionDAO.getTransactionsTarget(loginId);
		model.addAttribute("transactionTarget", transactionTarget);
		
		return "profile";
	}
	
	@RequestMapping(value="/editInformation")
	public String editProfile(Model model, @ModelAttribute("customer") Customer customer, BindingResult rs){
		customerDAO.updateCustomer(customer);
		model.addAttribute("customer", customer);
		model.addAttribute("message", "Update Information Successfully");
		return "profile";
	}
	
	@RequestMapping(value="/changePassword")
	public String changePasseword(Model model, @ModelAttribute("customer") Customer customer, BindingResult rs){
		BCryptPasswordEncoder encode = new BCryptPasswordEncoder();
		customer.setPassword(encode.encode(customer.getPassword()));
		customerDAO.updateCustomer(customer);
		model.addAttribute("customer", customer);
		model.addAttribute("message", "Update Password Successfully");
		return "profile";
	}
	
	@RequestMapping(value="/search", method=RequestMethod.POST)
	public String search(HttpServletRequest request, final RedirectAttributes attr){
		try {
			String fromDate = request.getParameter("fromDate");
			String toDate = request.getParameter("toDate");
			DateFormat format1 = new SimpleDateFormat("MM/dd/yyyy");
			
			Date from = format1.parse(fromDate);
			Date to = format1.parse(toDate);
			
			DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
			String fromString = format2.format(from);
			String toString = format2.format(to);
			
			Date dateFrom = format2.parse(fromString);
			Date dateTo = format2.parse(toString);
			
			List<Transaction> result = transactionDAO.searchByDate(dateFrom, dateTo);
			attr.addFlashAttribute("fromDate", fromDate);
			attr.addFlashAttribute("toDate", toDate);
			attr.addFlashAttribute("result", result);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "redirect:/profile";
	}
	
}
